package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 新增用户详情(完善资料)
     *
     * @param userInfo
     * @param token
     * @return
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
        Long userId = UserHolder.getUserId();

        userInfo.setId(userId);
        //新增用户详情
        userInfoService.save(userInfo);

        return ResponseEntity.ok(null);
    }

    /**
     * 上传头像-补充头像
     *
     * @param headPhoto
     * @return
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity loginReginfoHead(MultipartFile headPhoto) throws IOException {

        String filename = headPhoto.getOriginalFilename();
        InputStream inputStream = headPhoto.getInputStream();

        userInfoService.deleteHeadPhoto(filename, inputStream);

        return ResponseEntity.ok(null);
    }
}
